clear all 
set more off

* this file creates creates tenure-exit profiles

* graph setups
set autotabgraphs on

local msize1 "medsmall"
local msize2 "medlarge"

local color1 "0 76 167"
local color2 "146 0 0"
local color3 "62 150 81"
local colorgb "white"

local color1b "0 76 167"
local color2b "146 0 0"
local color3b "62 150 81"

* load
use "Tenure_Transitions.dta", clear 

* regression reported directly in results window if you want to 
* check whether differences in regression lines are statistically significant
encode(trans), gen(trans_n)
reg ytrate i.trans_n c.lcgdp#i.trans_n if trans == "W1M" | trans == "W2M" | trans == "W3M" | trans == "W4M", cluster(ccode)
test _b[2b.trans_n#c.lcgdp] == _b[5.trans_n#c.lcgdp]
test _b[2b.trans_n#c.lcgdp] == _b[8.trans_n#c.lcgdp]
test _b[2b.trans_n#c.lcgdp] == _b[11.trans_n#c.lcgdp]
test _b[5.trans_n#c.lcgdp] == _b[8.trans_n#c.lcgdp]
test _b[5.trans_n#c.lcgdp] == _b[11.trans_n#c.lcgdp]
test _b[8.trans_n#c.lcgdp] == _b[11.trans_n#c.lcgdp]


* graph stuff
local lab1 "0-6 months"
local lab2 "6-12 months"
local lab3 "1-5 years"
local lab4 "5+ years"

* compute trends for exit
forvalues j = 1/4 {
	reg ytrate lcgdp if trans == "W`j'M"
	predict trend_W`j'M
}
	
	
* this code will let you loop different graphs. 
* Change tlist for whetever the q+1 employment state is.
local tlist "M"  		/* outcome */
local ymax = 0.4		/* max y value for graph */
local ycut = 0.1		/* y variable spacing for graph */

* averages
bys ccode trans: egen mean_ytrate = mean(ytrate)
bys ccode trans: egen mean_AGDP = mean(AGDP)
bys ccode trans: gen tencounter = _n

* loop over whatever graphs you want to produce.
* careful if you want to expand beyond paper: 
* they all currently get saved as "Figure9.pdf" so will overwrite whatever came before.
foreach x of local tlist {
	
	gsort+ AGDP
	#delimit ;
	twoway
	(line trend_W1`x' AGDP if trans == "W1`x'", lcolor(black%50) lwidth(medthin) alc(background) lpattern(dash))
	(line trend_W2`x'  AGDP if trans == "W2`x'", lcolor("`color1'%50") lwidth(medthin) alc(background) lpattern(solid))
	(line trend_W3`x'  AGDP if trans == "W3`x'", lcolor("`color2'%50") lwidth(medthin) alc(background) lpattern(longdash_dot))
	(line trend_W4`x'  AGDP if trans == "W4`x'", lcolor("`color3'%50") lwidth(medthin) alc(background) lpattern(solid))
	(scatter ytrate AGDP if trans == "W1`x'", mcolor("black%15") msize(`msize1')  msymbol(circle_hollow) mlwidth(thin))
	(scatter ytrate AGDP if trans == "W2`x'", mcolor("`color1'%15") msize(`msize1')  msymbol(diamond_hollow) mlwidth(thin))
	(scatter ytrate AGDP if trans == "W3`x'", mcolor("`color2'%15") msize(`msize1')  msymbol(triangle_hollow) mlwidth(thin))
	(scatter ytrate AGDP if trans == "W4`x'", mcolor("`color3'%15") msize(`msize1')  msymbol(square_hollow) mlwidth(thin))
	(scatter mean_ytrate mean_AGDP if trans == "W1`x'" & tencounter == 1, mcolor(black) msize(`msize2') mlabcolor(black) msymbol(O))
	(scatter mean_ytrate mean_AGDP if trans == "W2`x'" & tencounter == 1, mcolor("`color1'") msize(`msize2') mlabcolor("`color1'") msymbol(diamond))
	(scatter mean_ytrate mean_AGDP if trans == "W3`x'" & tencounter == 1, mcolor("`color2'") msize(`msize2') mlabcolor("`color2'") msymbol(triangle))
	(scatter mean_ytrate mean_AGDP if trans == "W4`x'" & tencounter == 1, mcolor("`color3'") msize(`msize2') mlabcolor("`color3'") msymbol(square))
	,
	name(T_W`x') legend(order(9 1 10 2 11 3 12  4) cols(2)  size(*1)
	subtitle("Tenure:   ", position(9) size(*1)) label(9 "`lab1'") label(10 "`lab2'") label(11 "`lab3'") label(12 "`lab4'")
	label(1 "fitted") label(2 "fitted") label(3 "fitted") label(4 "fitted")
	)
	graphregion(color("`colorgb'")) xtitle("GDP per capita") xscale(log) 
	xlabel(4000 8000 16000 32000 64000)
	yscale(r(0 `ymax')) ylabel(0(`ycut')`ymax'); 
	graph export "Figures/Figure9.tif", replace ;
	graph export "Figures/pdf_figures/Figure9.pdf", replace ;
	
	#delimit cr
	
	* ------------------- alternative breaking into two
	/*
	gsort+ AGDP
	#delimit ;
	twoway
	(line trend_W1`x' AGDP if trans == "W1`x'", lcolor(black%50) lwidth(medthin) alc(background) lpattern(longdash_dot))
	(line trend_W2`x'  AGDP if trans == "W2`x'", lcolor("`color1'%50") lwidth(medthin) alc(background) lpattern(solid))
	(scatter ytrate AGDP if trans == "W1`x'", mcolor("black%15") msize(`msize1')  msymbol(circle) mlwidth(none))
	(scatter ytrate AGDP if trans == "W2`x'", mcolor("`color1'%15") msize(`msize1')  msymbol(diamond) mlwidth(none))
	(scatter mean_ytrate mean_AGDP if trans == "W1`x'" & tencounter == 1, mcolor(black) msize(`msize2') mlabcolor(black) msymbol(O) mlabel(ccode))
	(scatter mean_ytrate mean_AGDP if trans == "W2`x'" & tencounter == 1, mcolor("`color1'") msize(`msize2') mlabcolor("`color1'") msymbol(diamond))
	,
	name(T_W`x'a) legend(order(5 6 1 2) cols(2) 
	subtitle("Tenure:   ", position(9)) label(5 "`lab1'") label(6 "`lab2'") 
	label(1 "best fit") label(2 "best fit")
	)
	graphregion(color("`colorgb'")) xtitle("GDP per capita") xscale(log) 
	xlabel(4000 8000 16000 32000 64000)
	yscale(r(0 `ymax')) ylabel(0(`ycut')`ymax'); 
	graph export "Figures/Figure9a_alt.eps", replace ;
	graph export "Figures/pdf_figures/Figure9a_alt.pdf", replace ;
	
	#delimit cr
	

	gsort+ AGDP
	#delimit ;
	twoway
	(line trend_W3`x'  AGDP if trans == "W3`x'", lcolor("`color2'%50") lwidth(medthin) alc(background) lpattern(longdash_dot))
	(line trend_W4`x'  AGDP if trans == "W4`x'", lcolor("`color3'%50") lwidth(medthin) alc(background) lpattern(solid))
	(scatter ytrate AGDP if trans == "W3`x'", mcolor("`color2'%15") msize(`msize1')  msymbol(circle) mlwidth(none))
	(scatter ytrate AGDP if trans == "W4`x'", mcolor("`color3'%15") msize(`msize1')  msymbol(diamond) mlwidth(none))
	(scatter mean_ytrate mean_AGDP if trans == "W3`x'" & tencounter == 1, mcolor("`color2'") msize(`msize2') mlabcolor("`color2'") msymbol(circle) mlabel(ccode))
	(scatter mean_ytrate mean_AGDP if trans == "W4`x'" & tencounter == 1, mcolor("`color3'") msize(`msize2') mlabcolor("`color3'") msymbol(diamond))
	,
	name(T_W`x'b) legend(order(5 6 1 2) cols(2) 
	subtitle("Tenure:   ", position(9)) label(5 "`lab3'") label(6 "`lab4'") 
	label(1 "best fit") label(2 "best fit")
	)
	graphregion(color("`colorgb'")) xtitle("GDP per capita") xscale(log) 
	xlabel(4000 8000 16000 32000 64000)
	yscale(r(0 `ymax')) ylabel(0(`ycut')`ymax'); 
	graph export "Figures/Figure9b_alt.eps", replace ;
	graph export "Figures/pdf_figures/Figure9b_alt.pdf", replace ;
	
	#delimit cr	
	*/
}


